package com.jiantong.web.controller.news;

import com.jiantong.news.model.News;
import com.jiantong.news.service.NewsReadService;
import com.jiantong.news.service.NewsWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * Created by niufei on 2017/5/2.
 *
 */
@RestController
@Slf4j
public class NewsController {

    @RpcConsumer
    private NewsWriteService newsWriteService;

    @RpcConsumer
    private NewsReadService newsReadService;

    @PostMapping(value = "/api/create/news")
    public Long createNews(@RequestBody News news){
        if(news.getId() != null){
            log.error("this news is exist");
            throw new OPClientException("id is exist");
        }
        Response<Long> response = newsWriteService.create(news);
        if(!response.isSuccess()){
            log.error("failed to create news case = {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @PutMapping(value = "/api/update/news")
    public Boolean updateNews(@RequestBody News news){
        if(news.getId() == null){
            log.error("this id is not exist");
            throw new OPClientException("id is not exist");
        }
        Response<Boolean> newsResponse = newsWriteService.update(news);
        if(!newsResponse.isSuccess()){
            log.error("failed to update news case = {}",newsResponse.getError());
            throw new OPServerException(newsResponse.getError());
        }
        return newsResponse.getResult();
    }

    @GetMapping(value = "/api/paging/news")
    public Paging<News> pagingNews(@RequestParam Integer pageNo,
                                   @RequestParam Integer pageSize,
                                   @RequestParam Integer type){
        Response<Paging<News>> response = newsReadService.pagingNews(pageNo,pageSize,type);
        if(!response.isSuccess()){
            log.error("failed to paging news case = {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @GetMapping(value = "/api/find/news/by/id")
    public News findNewsById(@RequestParam Long id){
        if(id == null){
            log.error("this id is not exist");
            throw new OPClientException("id is not exist");
        }
        Response<News> response = newsReadService.finById(id);
        if(!response.isSuccess()){
            log.error("failed to find news by id = {} , case = {}",id,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }
    @GetMapping(value = "/api/find/news/by/{id}")
    public News findNewsByIdTest(@PathVariable Long id){
        if(id == null){
            log.error("this id is not exist");
            throw new OPClientException("id is not exist");
        }
        Response<News> response = newsReadService.finById(id);
        if(!response.isSuccess()){
            log.error("failed to find news by id = {} , case = {}",id,response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }
}
